package com.smarthome.core.housemanager.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShBuildingService;
import com.smarthome.base.service.ShResidenceService;
import com.smarthome.core.housemanager.BuildingManagerService;
import com.smarthome.domain.bean.ShBuilding;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * 
 * @author dhc
 * @date 2015年9月14日
 */
@Service("buildingManagerService")
public class BuildingManagerServiceImpl implements BuildingManagerService{

	@Resource(name="shBuildingService")
	private ShBuildingService buildingService;
	
	@Resource(name="shResidenceService")
	private ShResidenceService shResidenceService;
	@Override
	public DataGrid getDataGrid(ShBuilding building, PageHelper helper) {
		return buildingService.getDataGrid(building, helper);
	}

	@Override
	public ShBuilding querybuildingById(Long id) {
		return buildingService.selectEntryById(id);
	}

	@Override
	public int add(ShBuilding building) {
		return buildingService.insert(building);
	}

	@Override
	public int edit(ShBuilding building) {
		ShBuilding saveBuilding = buildingService.selectEntryById(building.getId());
		if(saveBuilding != null){
			BeanUtils.copyProperties(building, saveBuilding);
			 buildingService.saveOrUpdate(saveBuilding);
		}
		
		return 0;
	}

	@Override
	public int delte(Long id) {
		return buildingService.deleteByIds(id);
	}

	@Override
	public List<ShBuilding> queryShBuildings(ShBuilding building) {
		return buildingService.selectEntryList(building);
	}

	@Override
	public boolean isRepeat(ShBuilding building) {
		if(null == building) return false;
		
		ShBuilding qn = new ShBuilding();
		qn.setName(building.getName());
		qn.setResidenceId(building.getResidenceId());
		List<ShBuilding> buildings = buildingService.selectEntryList(qn);
		if(buildings == null || buildings.size() == 0){
			return false;
		}
		

		for(ShBuilding s : buildings){
			if((s.getName() == null  && building.getName() == null)||s.getName().equals(building.getName())  ){
				if(building.getId() != null && s.getId().equals(building.getId())) continue;
				return true;
			}
		}
		
		return false;
	}

	/* (non-Javadoc)
	 * @see com.smarthome.core.housemanager.BuildingManagerService#queryShBuildingsByResidenceId(java.lang.Long)
	 */
	@Override
	public List<ShBuilding> queryShBuildingsByResidenceId(Long residenceId) {
		if(null == residenceId) return null;
		ShBuilding b = new ShBuilding();
		b.setResidenceId(residenceId);
		
		return buildingService.selectEntryList(b);
	}
	@Override
	public List<Combobox> queryComboboxByResidenceId(Long residenceId) {
		List<Combobox> boxs = new ArrayList<Combobox>();
		if(null == residenceId) return boxs;
		ShBuilding b = new ShBuilding();
		b.setResidenceId(residenceId);
		List<ShBuilding> bs = buildingService.selectEntryList(b);
		
		if(bs != null){
			for(ShBuilding b_ : bs){
				Combobox box =new Combobox(b_.getId()+"",b_.getName());
				boxs.add(box);
			}
		}
		return boxs;
	}

}
